package com.ctgu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctgu.common.ResponseResult;
import com.ctgu.mapper.MenuMapper;
import com.ctgu.model.entity.*;
import com.ctgu.model.vo.EmployeeVo;
import com.ctgu.service.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;


@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IEmployeeService employeeService;

    @Autowired
    private IPositionService positionService;

    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private MenuMapper menuMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        String role= String.valueOf(username.charAt(username.length()-1));
        username=username.substring(0,username.length()-1);
        //超级管理员登入逻辑
        if(role.equals("0")){
            LambdaQueryWrapper<Admin> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(Admin::getUserName,username);
            Admin admin=adminService.getOne(wrapper);
            if(Objects.isNull(admin)){
                throw new RuntimeException("用户名不存在");
            }
            List<String> list=menuMapper.selectPermsByAdminId(admin.getId());
            return new LoginUser(admin,list);
        }
        //公司管理员登入逻辑
        if(role.equals("1")){
            LambdaQueryWrapper<Company> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(Company::getUserName,username);
            Company company=companyService.getOne(wrapper);
            if(Objects.isNull(company)){
                throw new RuntimeException("用户名不存在");
            }
            if(company.getStatus().equals("0")){
                throw new RuntimeException("账户静止登入");
            }
            List<String> list=menuMapper.selectPermsByCompanyId(company.getId());
            return new LoginUser(company,list);
        }
        //员工登入逻辑
        if(role.equals("2")){
            LambdaQueryWrapper<Employee> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(Employee::getUserName,username);
            Employee employee=employeeService.getOne(wrapper);
            EmployeeVo employeeVo=new EmployeeVo();
            BeanUtils.copyProperties(employee,employeeVo);
            employeeVo.setDeptName(departmentService.getById(employee.getDepId()).getName());
            employeeVo.setPosName(positionService.getById(employee.getPosId()).getName());
            employeeVo.setCompanyName(companyService.getById(employee.getCompanyId()).getCompanyName());
            if(Objects.isNull(employee)){
                throw new RuntimeException("用户名不存在");
            }
            if(employee.getStatus().equals("0")){
                throw new RuntimeException("账户静止登入");
            }
            List<String> list=menuMapper.selectPermsByEmployeeId(employee.getId());
            return new LoginUser(employeeVo,list);
        }
//        // 查询用户信息
//        LambdaQueryWrapper<> wrapper=new LambdaQueryWrapper<>();
//        wrapper.eq(User::getUserName,username);
//        User one = userService.getOne(wrapper);
//        if(Objects.isNull(one)){
//            throw new RuntimeException("用户名不存在");
//        }
//        //TODO 查询对应权限信息
//        List<String> list=menuMapper.selectPermsByUserId(one.getId());
//
//        //把数据封装成UserDetails返回
//        return new LoginUser(one,list);
        return null;
    }
}
